package com.eling.elcms.travel.model;

import com.eling.elcms.basedoc.model.EnumSerializer;
import com.eling.elcms.core.dao.annotation.CriteriaEnhancer;
import com.eling.elcms.core.dao.annotation.CriteriaEnhancers;
import com.eling.elcms.core.dao.annotation.In;
import com.eling.elcms.core.dao.hibernate.OrderCriteriaEnhancer;
import com.eling.elcms.core.dao.hibernate.search.EnumBridge;
import com.eling.elcms.core.model.BaseModel;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.hibernate.search.annotations.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/** 产品定义（详细）
 * 
 * @author linjizeng
 * 
 * 模型评审：新增模型，wulina 2016-09-07
 *
 */
@Entity
@Table(name = "travel_productruledetail")
@Indexed
@CriteriaEnhancers(criteriaEnhancers = {
		@CriteriaEnhancer(impl = OrderCriteriaEnhancer.class)})
public class ProductRuleDetail extends BaseModel {

	private static final long serialVersionUID = 4443372196528046418L;

	@Id
	@DocumentId
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@In
	private Long pkProductRuleDetail;
	
	/** 产品 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pkProductRule")
	@IndexedEmbedded(depth = 1)
	private ProductRule productRule;
	
	/** 产品类型 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pkProductType")
	@IndexedEmbedded(depth = 1)
	private ProductType productType;
	
	/** 产品批次 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pkProductBatch")
	@IndexedEmbedded(depth = 1)
	private ProductBatch productBatch;
	
	/** 产品价格 */
	@Column
	@Field
	private Double price;
	
	/** 有效期 */
	@Column
	@Field
	private Integer validity;
	
	/** 居住期限 */
	@Column
	@Field
	private Integer liveDays;
	
	/** 期限单位 */
	@Enumerated(EnumType.STRING)
	@Field
	@In
	@FieldBridge(impl = EnumBridge.class)
	private Unit unit;
	
	@JsonSerialize(using = EnumSerializer.class)
	public static enum Unit {
		Month("月"),
		Day("天/间");
		private String display;
		Unit(String display) {
			this.display = display;
		}
		public String getDisplay() {
			return display;
		}
	}
	
	/** 规则 */
	@ManyToMany
	@JoinTable(name = "travel_pd_settlementsrule",
	joinColumns = {@JoinColumn(name = "pkProductRuleDetail")},
	inverseJoinColumns = {@JoinColumn(name = "pkSettlementsRule")})
	private Set<SettlementsRule> settlementsRules;
	
	/** 适用范围 */
	@ManyToMany
	@JoinTable(name = "travel_pd_rulescope",
	joinColumns = {@JoinColumn(name = "pkProductRuleDetail")},
	inverseJoinColumns = {@JoinColumn(name = "pkRuleScope")})
	private Set<RuleScope> ruleScopes;
	
	/**  封存*/
	@Column
	@Field
	private Boolean seal;
	
	/** 描述 */
	@Column(length = 1023)
	@Field
	private String description;

	@Version
	private Integer version;

	public Long getPkProductRuleDetail() {
		return pkProductRuleDetail;
	}

	public void setPkProductRuleDetail(Long pkProductRuleDetail) {
		this.pkProductRuleDetail = pkProductRuleDetail;
	}

	public ProductRule getProductRule() {
		return productRule;
	}

	public void setProductRule(ProductRule productRule) {
		this.productRule = productRule;
	}

	public ProductType getProductType() {
		return productType;
	}

	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	public ProductBatch getProductBatch() {
		return productBatch;
	}

	public void setProductBatch(ProductBatch productBatch) {
		this.productBatch = productBatch;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Integer getValidity() {
		return validity;
	}

	public void setValidity(Integer validity) {
		this.validity = validity;
	}

	public Integer getLiveDays() {
		return liveDays;
	}

	public void setLiveDays(Integer liveDays) {
		this.liveDays = liveDays;
	}

	public Unit getUnit() {
		return unit;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Set<SettlementsRule> getSettlementsRules() {
		return settlementsRules;
	}

	public void setSettlementsRules(Set<SettlementsRule> settlementsRules) {
		this.settlementsRules = settlementsRules;
	}

	public Set<RuleScope> getRuleScopes() {
		return ruleScopes;
	}

	public void setRuleScopes(Set<RuleScope> ruleScopes) {
		this.ruleScopes = ruleScopes;
	}

	public Boolean getSeal() {
		return seal;
	}

	public void setSeal(Boolean seal) {
		this.seal = seal;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	/** 费用抵扣规则 */
	public SettlementsRule getExpenseSettlementsRule() {
		for (SettlementsRule rule : settlementsRules){
			if (rule.getType() == RuleType.Expense){				
				return rule;
			}
		}
		return null;
	}
	/** 费用抵扣范围 */
	public List<RuleScope> getExpenseRuleScope() {
		List<RuleScope> result = new ArrayList<RuleScope>();
		for (RuleScope scope : ruleScopes){
			if (scope.getRuleType() == RuleType.Expense){				
				result.add(scope);
			}
		}
		return result;
	}
	/** 床位补贴规则 */
	public SettlementsRule getBedSubsidySettlementsRule() {
		for (SettlementsRule rule : settlementsRules){
			if (rule.getType() == RuleType.BedSubsidy){				
				return rule;
			}
		}
		return null;
	}
	/** 床位补贴范围 */
	public List<RuleScope> getBedSubsidyRuleScope() {
		List<RuleScope> result = new ArrayList<RuleScope>();
		for (RuleScope scope : ruleScopes){
			if (scope.getRuleType() == RuleType.BedSubsidy){				
				result.add(scope);
			}
		}
		return result;
	}
	/** 福利券发放规则 */
	public SettlementsRule getCouponSettlementsRule() {
		for (SettlementsRule rule : settlementsRules){
			if (rule.getType() == RuleType.Coupon){				
				return rule;
			}
		}
		return null;
	}
	/** 福利券发放范围 */
	public List<RuleScope> getCouponRuleScope() {
		List<RuleScope> result = new ArrayList<RuleScope>();
		for (RuleScope scope : ruleScopes){
			if (scope.getRuleType() == RuleType.Coupon){				
				result.add(scope);
			}
		}
		return result;
	}
}
